#include "stdafx.h"
#include "Ruler.h"
#include "Gdi.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Ruler::Ruler()
{
   brush.CreateSolidBrush(Sheet::colorText);
}

Ruler::~Ruler()
{
	brush.DeleteObject();
}

CPoint Ruler::GetO()
{
	return CPoint(m_bounding.left+10, m_bounding.bottom-60);
}

void Ruler::DrawRuler(CDC *pDC)
{
    CPen *pOldPen = pDC->SelectObject(&Sheet::txpen);

	CPoint o = GetO();
    pDC->MoveTo(o);
	pDC->LineTo(m_bounding.right, o.y);
	pDC->MoveTo(o.x, m_bounding.top);
	pDC->LineTo(o.x, o.y);

	pDC->SelectObject(pOldPen);
}

CRect Ruler::GetItemRect(int N, int Y)
{
	CPoint o = GetO();

    float width  = m_bounding.Width() / 37;
    int   height = 4;

	CRect rect;

	rect.left = o.x + 1 + width * N;
	rect.right = rect.left + width - 1;
	rect.bottom = o.y + 2 - Y * height;
	rect.top = rect.bottom - height + 1;

	if(N==0){
		rect.left += 1;
	}

	return rect;
}

void Ruler::DrawColumn(CDC *pDC, int N, int H)
{
   for(int i=1; i<=H; i++)
   {
      DrawItem(pDC, N, i);
   }
}

void Ruler::DrawItem(CDC *pDC, int N, int Y)
{
    CRect rect = GetItemRect(N, Y);

    // out, do not draw
	if((rect.top)>0)
	{
		pDC->FillRect(&rect, &brush);
	}
}


void Ruler::ShowData(CDC *pDC)
{
    SortPool* sortPool = (SortPool*)DataServer::getInstance()->getPool(DataPool::Sort);

    int count = sortPool->count();
    for(int i=0; i<count; i++)
    {
        DrawColumn(pDC, i, sortPool->GetItemAt(i).second);
    }
}
